/******************************************************************************
 * Copyright (C) 2018 ShenZhen ComTop Information Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为深圳康拓普开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、
 * 复制、修改或发布本软件.
 ******************************************************************************/

package com.comtop.lcam.fwms.managementkey.socket;

/**
 * FIXME 类注释信息(此标记自动生成,注释填写完成后请删除)
 * 
 * <pre>
 * [
 * 调用关系:
 * 实现接口及父类:
 * 子类:
 * 内部类列表:
 * ]
 * </pre>
 * 
 * @author 李锋
 * @since 1.0
 * @version 2018-11-27 李锋
 */
public final class EslcsSocketHexUtil {
    
    /**
     * 构造函数
     */
    private EslcsSocketHexUtil() {
        
    }
    
    /**
     * FIXME 方法注释信息(此标记由Eclipse自动生成,请填写注释信息删除此标记)
     * 
     * @param inHex
     * @return byte
     */
    static public byte[] HexToByteArr(String inHex) {
        int hexlen = inHex.length();
        byte[] result;
        if (isOdd(hexlen) == 1) {
            hexlen++;
            result = new byte[(hexlen / 2)];
            inHex = "0" + inHex;
        } else {
            result = new byte[(hexlen / 2)];
        }
        int j = 0;
        for (int i = 0; i < hexlen; i += 2) {
            result[j] = HexToByte(inHex.substring(i, i + 2));
            j++;
        }
        return result;
    }
    
    /**
     * FIXME 方法注释信息(此标记由Eclipse自动生成,请填写注释信息删除此标记)
     * 
     * @param inByte
     * @return String
     */
    static public String Byte2Hex(Byte inByte) {
        return String.format("%02x", inByte).toUpperCase();
    }
    
    /**
     * FIXME 方法注释信息(此标记由Eclipse自动生成,请填写注释信息删除此标记)
     * 
     * @param num
     * @return int
     */
    public static int isOdd(int num) {
        return num & 0x1;
    }
    
    /**
     * 十六进制数inHex的十进制
     * 
     * @param inHex
     * @return byte
     */
    public static byte HexToByte(String inHex) {
        return (byte) Integer.parseInt(inHex, 16);
    }
    
    /**
     * FIXME 方法注释信息(此标记由Eclipse自动生成,请填写注释信息删除此标记)
     * 
     * @param inHex
     * @return byte
     */
    public static int HexToInt(String inHex)//
    {
        return Integer.parseInt(inHex, 16);// 十六进制数inHex的十进制
    }
    
    /**
     * FIXME 方法注释信息(此标记由Eclipse自动生成,请填写注释信息删除此标记)
     * 
     * @param bufferString
     * @return byte
     */
    public static byte xor(String bufferString) {
        byte[] buffer = HexToByteArr(bufferString);
        byte sum = 0;
        for (int i = 0; i < buffer.length; i++) {
            sum = (byte) (sum ^ buffer[i]);
            
        }
        return sum;
    }
    
    /**
     * 异或校验
     * 
     * @param bufferString bufferString
     * @return static
     */
    public static String xorBy16(String bufferString) {
        return Byte2Hex(xor(bufferString));
    }
    
    /**
     * FIXME 方法注释信息(此标记由Eclipse自动生成,请填写注释信息删除此标记)
     * 
     * @param inHex
     * @return byte
     */
    public static String[] HexToStringArr(String inHex) {
        int hexlen = inHex.length();
        String[] result;
        if (isOdd(hexlen) == 1) {
            hexlen++;
            result = new String[(hexlen / 2)];
            inHex = "0" + inHex;
        } else {
            result = new String[(hexlen / 2)];
        }
        int j = 0;
        for (int i = 0; i < hexlen; i += 2) {
            result[j] = inHex.substring(i, i + 2);
            j++;
        }
        return result;
    }
    
    /**
     * FIXME 方法注释信息(此标记由Eclipse自动生成,请填写注释信息删除此标记)
     * 
     * @param inHex
     * @param beginIndex
     * @param endIndex
     * @return String
     */
    public static String subStr(String[] inHex, int beginIndex, int endIndex) {
        String str = "";
        if (null != inHex && inHex.length > 0) {
            for (int i = 0; i < inHex.length; i++) {
                if (i >= beginIndex && i < endIndex) {
                    str += inHex[i];
                }
            }
        }
        return str;
        
    }
    
    /**
     * 字节数组转成hex
     * 
     * @param var0
     * @return String
     */
    public static String byteArrayToHex(byte[] var0) {
        if (var0 != null && var0.length != 0) {
            StringBuilder var1 = new StringBuilder(var0.length);
            
            for (int var2 = 0; var2 < var0.length; ++var2) {
                var1.append(String.format("%02X", new Object[] { Byte.valueOf(var0[var2]) }));
                // if(var2 < var0.length - 1) {
                // var1.append(' ');
                // }
            }
            
            return var1.toString();
        } else {
            return "";
        }
    }
}
